import {UserOutlined, InfoCircleOutlined} from '@ant-design/icons-vue';
import {defineComponent, onMounted, ref} from 'vue';
import {reactive} from 'vue'
import {useRouter} from 'vue-router';
import {sysLogin,selectUserListByAdmin,addUser} from '../api/Login'
import { message } from 'ant-design-vue';
export default defineComponent({
    components: {
        UserOutlined,
        InfoCircleOutlined,
    },

    setup() {
        const formState = reactive({
            accountNumber: '',
            password: '',
        });
        const list = ref([])
        const formRef = ref();

        let validatePass = async (_rule, value) => {
            if (value === '') {
                return Promise.reject('请输入用用户名');
            } else {
                return Promise.resolve();
            }
        };

        let validatePass2 = async (_rule, value) => {
            if (value === '') {
                return Promise.reject('请确认登录密码');
            } else if (value !== formState.password) {
                return Promise.reject("两次密码输入不一致");
            } else {
                return Promise.resolve();
            }
        };
        let validatepassword = async (_rule, value) => {
            if (value === '') {
                return Promise.reject('请输入登录密码')
            } else {
                return Promise.resolve();
            }
        };
        let validatephone = async (_rule, value) => {
            if (value === '') {
                return Promise.reject('请输入手机号')
            } else if (!/^1[2-9]\d{9}$/.test(value)) {
                return Promise.reject('手机号格式不正确');
            } else {
                return Promise.resolve();
            }
        }
        const rules = {
            username: [{
                required: true,
                validator: validatePass,
                trigger: 'change',
            }],
            checkPass: [{
                validator: validatePass2,
                trigger: 'change',
            }],
            password: [{
                required: true,
                validator: validatepassword,
                trigger: 'change',
            }],
            phone: [
                {
                    required: true,
                    validator: validatephone,
                    trigger: 'change',
                }
            ]
        };

        const handleFinish = values => {
            console.log(values, formState);
        };

        const handleFinishFailed = errors => {
            console.log(errors);
        };

        const resetForm = () => {
            formRef.value.resetFields();
        };

        const onFinishFailed = errorInfo => {
            console.log('Failed:', errorInfo);
        };
        const $router = useRouter();
        const register = ref(false);
        const logIn = ref(true);
        const timer = ref('获取验证码')
        const downTime = () => {
            let TIME_COUNT = 60;
            if (timer) {
                let timers = setInterval(() => {
                    if(TIME_COUNT > 0 ){
                        timer.value = TIME_COUNT--  + '秒重新获取';
                    }else {
                        timer.value = '获取验证码'
                    }
                }, 1000)
            }
        }
        let goregister = () => {
            register.value = true
            logIn.value = false
        }
        let gologIn = () => {
            
            register.value = false
            logIn.value = true
        }
        let addUserFun = () =>{
            console.log(formState)
            addUser(formState).then((res)=>{
                if(res.code == 200){
                   message.warning('注册成功')
 
                   onFinish()
                }
             })
        }
        // 登录跳转
        const onFinish = (values) => {
            console.log('Success:', formState);
            sysLogin(formState).then((res)=>{
                if(res.code == 200){
                    // 1就是母账号., 0就是子账号
                    sessionStorage.setItem('user',JSON.stringify(res.data))
                    if(res.data.level == 0){
                        $router.push({path: '/moodIndex'})
                    }else{
                        $router.push({path: '/largeSize'})
                    }
                   
                }
            })
        };
       
        onMounted(()=>{
            selectUserListByAdmin().then((res)=>{
                if(res.code == 200){
                    console.log(res.data)
                    list.value = res.data
                }
            })
        })
        return {
            formRef,
            rules,
            downTime,
            timer,
            list,
            handleFinishFailed,
            handleFinish,
            resetForm,
            formState,
            onFinish,
            onFinishFailed,
            register,
            addUserFun,
            logIn,
            goregister,
            gologIn,
        };
    },
});